package com.chinasoftinc.facommon.view.device.abilitypersent;

import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.MatchingSkills;
import ohos.wifi.WifiEvents;

import com.chinasoftinc.facommon.DeviceControlConstants;
import com.chinasoftinc.facommon.utils.RemoteUtils;
import com.chinasoftinc.facommon.view.device.ControlDeviceAbility;

/**
 * @since 2022-05-10
 */
public class WifiDevicePresent extends DeviceAbilityPresent {

    public WifiDevicePresent(ControlDeviceAbility ability) {
        super(ability);
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        String sessionId = intent.getStringParam("sessionId");
    }

    @Override
    protected void addSubscribeFaEvent(MatchingSkills matchingSkills) {
        // api文档和实际数据不匹配, 暂不启用
        // matchingSkills.addEvent(WifiEvents.EVENT_ACTIVE_STATE);
        // matchingSkills.addEvent(WifiEvents.EVENT_CONN_STATE);
    }

    @Override
    protected void onReceiveEvent(String action, CommonEventData commonEventData) {
        switch (action) {
            case WifiEvents.EVENT_ACTIVE_STATE: {
                // wifi开关变化
                IntentParams params = commonEventData.getIntent().getParams();
                if (params == null) {
                    return;
                }
                int wifiState = -1;
                if (params.hasParam(WifiEvents.PARAM_ACTIVE_STATE)) {
                    wifiState = (int) params.getParam(WifiEvents.PARAM_ACTIVE_STATE);
                }
                if (wifiState == -1 || conversationRemoteObj == null) {
                    return;
                }
                RemoteUtils.sendRemoveMessage(
                    conversationRemoteObj,
                    DeviceControlConstants.WIFI_ACTIVE_STATE_CHANGE,
                    "state", wifiState,
                    0
                );
            }
            break;
            case WifiEvents.EVENT_CONN_STATE: {
                // wifi连接状态变化
                IntentParams params = commonEventData.getIntent().getParams();
                if (params == null) {
                    return;
                }
                int wifiConnState = -1;
                if (params.hasParam(WifiEvents.PARAM_HOTSPOT_STATE)) {
                    wifiConnState = (int) params.getParam(WifiEvents.PARAM_HOTSPOT_STATE);
                }
                if (wifiConnState == -1 || conversationRemoteObj == null) {
                    return;
                }
                RemoteUtils.sendRemoveMessage(
                    conversationRemoteObj,
                    DeviceControlConstants.WIFI_CONN_STATE_CHANGE,
                    "state", wifiConnState,
                    0
                );
            }
            break;
        }
    }


    @Override
    public void onStop() {
        super.onStop();
    }
}
